<?php
/**
 * Ahthor: lf
 * Email: job@feehi.com
 * Blog: http://blog.feehi.com
 * Date: 2016/4/514:01
 */
namespace feehi\components;

use yii;
use yii\base\Component;
use common\models\Options;
use backend\models\SettingSmtpForm;
use yii\caching\FileDependency;

class Feehi extends Component{

    public $website_title = 'Feehi CMS';
    public $website_language = 'en-US';
    public $website_timezone = 'Asia/Shanghai';

    public function __construct()
    {
        parent::__construct();
        $cache = yii::$app->getCache();
        $key = 'options_system';
        if( ( $data=$cache->get($key) ) === false ){
            $data = Options::find()->where(['type'=>Options::TYPE_SYSTEM])->asArray()->indexBy("name")->all();
            $cacheDependencyObject = yii::createObject([
                'class' => 'feehi\helpers\FileDependencyHelper',
                'fileName' => 'options_system.txt',
                'rootDir' => '@backend/runtime/cache/file_dependency/',
            ]);
            $fileName = $cacheDependencyObject->createFile();
            $dependency = new FileDependency(['fileName' => $fileName]);
            $cache->set($key, $data, 0, $dependency);
        }
        foreach($data as $k => $v){
            $this->$k = $v['value'];
        }
        if( !$this->website_title ) $this->website_title = 'Feehi CMS';
        if( !$this->website_language ) $this->website_language = 'en-US';
        if( !$this->website_timezone ) $this->website_timezone = 'Asia/Shanghai';
    }

    public function init()
    {
        parent::init(); // TODO: Change the autogenerated stub
        $cache = yii::$app->getCache();
        $key = 'options_params';
        if( ( $data=$cache->get($key) ) === false ){
            $data = Options::find()->where(['type'=>Options::TYPE_CUSTOM, 'autoload'=>1])->asArray()->all();
            $cacheDependencyObject = yii::createObject([
                'class' => 'feehi\helpers\FileDependencyHelper',
                'fileName' => 'options_params.txt',
                'rootDir' => '@backend/runtime/cache/file_dependency/',
            ]);
            $fileName = $cacheDependencyObject->createFile();
            $dependency = new FileDependency(['fileName' => $fileName]);
            $cache->set($key, $data, 0, $dependency);
        }
        foreach ($data as $v){
            $this->{$v['name']} = $v['value'];
        }
    }

    public function __set($name, $value)
    {
        $this->$name = $value;
    }

    public function __get($name)
    {
        return $this->$name;
    }

    private static function setConfig()
    {
        if(!empty(yii::$app->feehi->website_url)) yii::$app->params['site']['url'] = yii::$app->feehi->website_url;
        if( substr(yii::$app->params['site']['url'], -1, 1) != '/' ) yii::$app->params['site']['url'] .= '/';
        if( stripos(yii::$app->params['site']['url'], 'http://') !== 0 && stripos(yii::$app->params['site']['url'], 'https://') !== 0 ) yii::$app->params['site']['url'] = "http://".yii::$app->params['site']['url'];
        $cache = yii::$app->getCache();
        $key = 'options_smtp';
        if( ( $smtpConfig=$cache->get($key) ) === false ){
            $smtpConfig = SettingSmtpForm::getComponentConfig();
            $cacheDependencyObject = yii::createObject([
                'class' => 'feehi\helpers\FileDependencyHelper',
                'fileName' => 'options_smtp.txt',
                'rootDir' => '@backend/runtime/cache/file_dependency/',
            ]);
            $fileName = $cacheDependencyObject->createFile();
            $dependency = new FileDependency(['fileName' => $fileName]);
            $cache->set($key, $smtpConfig, 0, $dependency);
        }
        if( !empty($smtpConfig['transport']['host']) && !empty($smtpConfig['transport']['username']) ) Yii::configure(yii::$app->mailer, $smtpConfig);
    }

    public static function setFrontendConfig()
    {
        self::setConfig();
        if(!yii::$app->feehi->website_status) yii::$app->catchAll = ['site/offline'];
        yii::$app->language = yii::$app->feehi->website_language;
        yii::$app->timeZone = yii::$app->feehi->website_timezone;
        if(!isset(yii::$app->params['site']['url']) || empty(yii::$app->params['site']['url'])) yii::$app->params['site']['url'] = yii::$app->request->getHostInfo();
    }

    public static function setBackendConfig()
    {
        self::setConfig();
    }

}